/**
 * Copyright (C) 2011-20077
 * 设计：		Zhangsk
 * 创建时间：	2011-12-24
 * Flash视频会议核心库 文档控制器类
 * 
 * //1.在登录会议成功之后，需要发送一条信令
 * //<iq type="get" to="207366{207366}@slavefs_1.machine1.com" id=""><query xmlns="meeting:share:browse"/></iq>
 * //上面已完成。
 * 
 * <iq type="get" to="503578{503578}@slavefs_1.machine1.com" id=""><query type="browse" xmlns="meeting:object:share"><info><jid>503578@masterfs_1.machine1.com/a2472425</jid><pageno>1</pageno><type>doc</type></info></query></iq>
 * 这个里面会把所有的笔画加在一起返回
 * 便于取得 当前共享文档的内容
 * 2.翻页之后，需要发送
 *   <iq type="get" to="503578{503578}@slavefs_1.machine1.com" id="">
re"><info><jid>503578@masterfs_1.machine1.com/a2472425</jid><pageno>2</pageno><type>doc</type></info></query></iq>
 *   
 * 然后这个会发送回这页上的所有笔画
 * 3.如果是画了一笔会收到
 * 
 * <iq from='503578{503578}@slavefs_1.machine1.com' id='' to='guest@masterjadc2s_1.machine1.com/{CBCA3443-CCA7-452C-9F16-257E43F972E0}' type='set' xmlns='jabber:client'><query type='create' xmlns='meeting:object:share'><info><jid>503578@masterfs_1.machine1.com/a2472425</jid><pageno>2</pageno><type>doc</type><object><shapobj>&lt;sharpe&gt;
  &lt;event_type&gt;shapeAdd&lt;/event_type&gt;
 */
 
package com.treemon.meetingcore.session
{
	import com.treemon.meetingcore.ExtObj.DocObj;
	import com.treemon.meetingcore.event.MeetingDocumentEvent;
	import com.treemon.meetingcore.event.MeetingEvent;
	
	import flash.utils.Dictionary;
	
	import mx.utils.*;
	
	
/*
<iq type="get" to="207366{207366}@slavefs_1.machine1.com" id=""><query type="browse" xmlns="meeting:object:share"><info><jid>207366@masterfs_1.machine1.com/a97020288</jid><pageno>0</pageno><type>doc</type></info></query></iq>
  在flash端 换页之后需要执行一下此信令，
  换文档的时候如果发现新建文档，则也需要执行，否则不需要执行。
  
*/
	
	public class DocumentControl extends ControlBase
	{
		public function DocumentControl(aSession: MeetingSession)
		{
			super(aSession);
		}
		
		override protected function addListener(): void
		{
			_session.addEventListener(MeetingEvent.DocumentShare, handleDocumentShare);
			_session.addEventListener(MeetingEvent.DocObjShare, handleDocObjShare);
			_session.addEventListener(MeetingEvent.DocShareBrowse, handleDocShareBrowse);
		}
		
		private var _docList: Dictionary = new Dictionary();
		 
		private function handleDocShareBrowse(aEvent: MeetingEvent): void 
		{
			trace('DocumentControl.handleMeetingShareBrowse');
			var xmlQuery: XML = aEvent.xml.children()[0];
	    	var ns: Namespace = xmlQuery.namespace();
	    	
			trace(xmlQuery.toString());
			var strType: String = xmlQuery.attribute("type");
			var strEventTyp:String="";

			if (strType == ""){trace('此处代码为完成,需要增加进入会议的时候,取得文档列表 error'); return;}
			//VideoTest.mxml可以接收
			if ("doc"==strType)
			{
				
			//	var strEventType: String;	
			    var strDocID:String ="";
			    var oDocObj:DocObj=null;
			    var strDownUrl: String=""; 
			    var strDocTitle:String="";
			    var strPageNo:String="";
			
			   	for(var j:int=0;j<xmlQuery.ns::info.length();j++)
			   	
	     	 	{
	     	 		
	     	 		 strDocID =xmlQuery.ns::info[j].ns::jid.toString();
	     	 		 oDocObj = _docList[strDocID];
	     	 		 strDownUrl = xmlQuery.ns::info[j].ns::downloadurl.toString();
	     	 		 strPageNo = xmlQuery.ns::info[j].ns::pagenum.toString();
	     	 		 strDocTitle = xmlQuery.ns::info[j].ns::doc.toString();
	     	 		 
	     	 		 if (oDocObj==null)
					{
						oDocObj = new DocObj(strDocID,strPageNo,strDownUrl);
						oDocObj.doc_title = strDocTitle;
						
						_docList[strDocID]=oDocObj;		
					}
	     	 		//_drawObj.appendChild(tmp.ns::object[j]);
	     	 		
	     	 		
	     	 	}	
	     	 	strEventTyp = MeetingDocumentEvent.DocInfo;
	     	 	var evtDocumentShare: MeetingDocumentEvent = new MeetingDocumentEvent(strEventTyp);
	     	 	evtDocumentShare._extInfo = xmlQuery.ns::info.toString();
	     	 	evtDocumentShare._ns = ns.uri;
	     	 	_session.dispatchEvent(evtDocumentShare);	
	     	 	
	     	 	
	     	 	
	     	 	
//			    for (var int
//				var strDocID: String = xmlQuery.ns::info.ns::jid.toString();			
//				v
//				var strPageNo: String = xmlQuery.ns::info.ns::pageno.toString();
//				= xmlQuery.ns::info.ns::downloadurl.toString();
//				var strShopeObj: String = xmlQuery.ns::info.ns::object.toString();
//				if (oDocObj==null)
//				{
//					oDocObj = new DocObj(strDocID,strPageNo,strDownUrl);
//					_docList[strDocID]=oDocObj;		
//				}
//				else
//				{
//					oDocObj.pageno = strPageNo;
//				}				
				
			}
		}		

		private function handleDocObjShare(aEvent: MeetingEvent):void 
		{
			trace('DocumentControl.handleMeetingObjectShare');
			var xmlQuery: XML = aEvent.xml.children()[0];
	    	var ns:Namespace = xmlQuery.namespace();
	    	
			trace(xmlQuery.toString());
			var strType: String = xmlQuery.attribute("type");
			var strEventType: String;		
			var strDocID: String = xmlQuery.ns::info.ns::jid.toString();			
			var oDocObj: DocObj = _docList[strDocID];
			var strPageNo: String = xmlQuery.ns::info.ns::pageno.toString();
			var strDownUrl: String = xmlQuery.ns::info.ns::downloadurl.toString();
			var strShopeObj: String = xmlQuery.ns::info.ns::object.toString();
			var strDocTitle: String = xmlQuery.ns::info.ns::doc.toString();
			if (null == oDocObj)
			{
				oDocObj = new DocObj(strDocID,strPageNo,strDownUrl);
				_docList[strDocID]=oDocObj;			
			}
			else
			{    
				oDocObj.pageno = strPageNo;
			}

			strEventType = MeetingDocumentEvent.DocObject;
			var evtDocumentShare: MeetingDocumentEvent = new MeetingDocumentEvent(strEventType);
		   
			if ("create" == strType)			
			{
				evtDocumentShare.drawobj = strShopeObj;
				evtDocumentShare.doc_jid = strDocID;
				evtDocumentShare.doctitle = oDocObj.doc_title;		
				
				evtDocumentShare.doc_url = oDocObj.DocURL();
				evtDocumentShare.pageno = oDocObj.pageno;
				//_session.dispatchEvent(evtDocumentShare);					
			}
			if ("browse" == strType)			
			{
				evtDocumentShare.drawobj = xmlQuery.ns::info.ns::object.toString();
				evtDocumentShare.doc_jid = strDocID;
					evtDocumentShare.doc_url = oDocObj.DocURL();
				evtDocumentShare.pageno = oDocObj.pageno;	
				evtDocumentShare.doctitle = oDocObj.doc_title;
							

			}			
			if (strType == ""){trace('DocumentControl.handle_meeting_object_share  error'); return;}
			_session.dispatchEvent(evtDocumentShare);
			

		//VideoTest.mxml可以接收
		}		
		/*
		meeting:doc:share
		在收到change的时候，在当前客户端还会手动啊一个下载下一页的信令
		受到change之后，发送
				<iq type="get" to="356283{356283}@slavefs_1.machine1.com" id="">
			<query type="browse" xmlns="meeting:object:share">
				<info><jid>356283@masterfs_1.machine1.com/a493797</jid><pageno>0</pageno><type>doc</type>
					
				</info></query></iq>
		   
		*/
		

		private function handleDocumentShare(aEvent: MeetingEvent):void 
		{
			trace('DocumentControl.handleDocumentShare');
			trace(aEvent.xml.toString());
			var xmlQuery: XML = aEvent.xml.children()[0];
	    	var ns: Namespace = xmlQuery.namespace();
			var strType: String = xmlQuery.attribute("type");
			var strEventType: String;		
			var strDocID: String = xmlQuery.ns::info.ns::jid.toString();
			var oDocObj: DocObj = _docList[strDocID];;
			var strPageNo: String = xmlQuery.ns::info.ns::pageno.toString();
			var strPageCount: String;
			var strDownUrl: String = xmlQuery.ns::info.ns::downloadurl.toString();
			var strShopeObj: String = xmlQuery.ns::info.ns::object.toString();
			var strDocTitle: String = xmlQuery.ns::info.ns::doc.toString();
			//var strDocTitle: String = xmlQuery.ns::info.ns::doc.toString();
			var strPlace:String="";
			
			trace("docid=="+strDocID+";"+strDownUrl);
			if (null == oDocObj)
			{
				oDocObj = new DocObj(strDocID,strPageNo,strDownUrl);
				_docList[strDocID]=oDocObj;				
			}
			else
			{    
				oDocObj.pageno = strPageNo;
			}

			if ("share" == strType)			//共享文档切换
			{
				strPageCount = xmlQuery.ns::info.ns::pagenum.toString();
				oDocObj.pageCount = strPageCount;
				strEventType = MeetingDocumentEvent.DocShare;

				var fileName:String=strDocTitle;//截取出输入的字符
				if (fileName!="")
				{
				    oDocObj.doc_title = fileName;
				}
			}
			else if ("change" == strType)	//显示共享文档的具体某一页
			{
				strEventType = MeetingDocumentEvent.DocChange;
				
				//需要发送一条browser信令给服务器。请求画笔动作
			}
			else if ("place" ==  strType)	//共享文档显示位置变化，用户滚动条的处理
			{
				strEventType = MeetingDocumentEvent.DocPlace;
				strPlace = xmlQuery.ns::info.ns::place.toString();
			}
			else if ("end" ==  strType)	//删除文档
			{
				strEventType = MeetingDocumentEvent.EndShare;
			}	
			else if ("browser" ==  strType)	//共享文档显示位置变化，用户滚动条的处理
			{
				strEventType = MeetingDocumentEvent.ShareBrowser;
			}
			else if ("download" == strType)  //下载
			
			{
				oDocObj.pageno=strPageNo;
				strEventType = MeetingDocumentEvent.DocDownload;
			}	
			if (strType == ""){trace('DocumentControl.handleDocumentShare  error'); return;}
			var evtDocumentShare: MeetingDocumentEvent = new MeetingDocumentEvent(strEventType);	

			evtDocumentShare.drawobj = strShopeObj;
			evtDocumentShare.doc_jid = strDocID;
			evtDocumentShare.doc_url = oDocObj.DocURL();
			evtDocumentShare.pageno = oDocObj.pageno;
			evtDocumentShare.pageCount = oDocObj.pageCount;
			evtDocumentShare.doctitle = oDocObj.doc_title;
			evtDocumentShare._place = strPlace;
			_session.dispatchEvent(evtDocumentShare);			//VideoTest.mxml可以接收
			if ("end" ==  strType)
			{
				_docList[strDocID] = null;
				delete _docList[strDocID];
			}
			if ("share" ==  strType)
			{
	     	 	strEventType = MeetingDocumentEvent.DocInfo;
	     	 	var evtDocumentShare: MeetingDocumentEvent = new MeetingDocumentEvent(strEventType);
	     	 	evtDocumentShare._extInfo = xmlQuery.ns::info.toString();
	     	 	evtDocumentShare._ns = ns.uri;
	     	 	_session.dispatchEvent(evtDocumentShare);	
			}			
			
			if ("change" == strType)//如果是换页,需要读取所有的笔画
			{
				//todo
				//改为调用方法，在session内部组织信令发送
				var s:String = mx.utils.StringUtil.substitute(MeetingEvent.MEETING_OBJECT_SHARE, _session.slaveFS,strDocID,strPageNo);
			// '<iq id="{0}" to="{1}" type="get"><query xmlns="jabber:iq:agents"/></iq>';        	 
				_session.sendXML(s);

				
			}
		}
					
	}
}